from fastapi import APIRouter,Depends,HTTPException
from fastapi import Request, status
from sqlalchemy import select, func,update
from sqlalchemy.orm import selectinload
from models.seckill import Seckill
from datetime import datetime
from schemas.response import ResultSchema, SeckillSchema, SeckillPageSchema,OrderSchema,OrderListSchema
from hooks.dependencies import get_db_session
from models import AsyncSession
from utils.auth import AuthHandler
from schemas.request import BuySchema
from models.order import Order
from alipay import AliPay
from alipay.utils import AliPayConfig
import settings
from alipay import AliPay
from alipay.utils import AliPayConfig

auth_handler = AuthHandler()

router = APIRouter(
    prefix="/order",
    tags=["订单"]
)

@router.get("/list",response_model=ResultSchema)
async def order_list(page:int=1,size:int=10,user_id = Depends(auth_handler.auth_access_dependency),session: AsyncSession = Depends(get_db_session)):
    async with session.begin():
        result=await session.execute(select(Order).options(selectinload(Order.seckill).selectinload(Seckill.commodity)).filter(Order.user_id==user_id).offset((page-1)*size).limit(size))    
        orders=result.scalars().all()
    order_list=[OrderSchema.model_validate(order) for order in orders]
        
    return ResultSchema(
        data=OrderListSchema(
            orders=order_list
        )
    )